import time

start_us = time.ticks_us()
ticks_debug = False #是否打印运行时间

def running_ticks_us(tag=""):
    global ticks_debug
    """
    tag： 打印标签
    当前运行时间us
    """
    end_us = time.ticks_us()
    diff = time.ticks_diff(end_us, start_us)
    if __debug__:
        print(f"{tag}: ", diff)
    return diff


import ubinascii
import machine
import gc
import os

from utils import platform, wlan

os_uname = os.uname()


def reboot():
    """重启设备"""
    machine.reset()


def get_id():
    """获取设备ID"""
    return ubinascii.hexlify(machine.unique_id()).decode("utf-8")


def get_vfs():
    KB = 1024
    stat = os.statvfs("/")
    size = stat[1] * stat[2]
    free = stat[0] * stat[3]
    used = size - free
    return {"size": size / KB, "free": free / KB, "used": used / KB}


info = {
    "device_id": get_id(),
    "platform": "-",
    "flash": {"size": "-"},
    "vfs": get_vfs(),
    "rtc": {"boot": "-"},
    "gc": {"free_m": "-", "alloc_m": "-"},
    "uname": {
        "sysname": os_uname.sysname,
        "nodename": os_uname.nodename,
        "release": os_uname.release,
        "version": os_uname.version,
        "machine": os_uname.machine,
    },
    "ifconfig": wlan.get_ifconfig(),
    "temperature": "-",
}


def get_platform():
    if platform.is_raspberry_pico():
        info["platform"] = "PICO"

    if platform.is_raspberry_pico2():
        info["platform"] = "PICO2"

    if platform.is_esp32_series():
        info["platform"] = "ESP32"

    if platform.is_esp8266():
        info["platform"] = "ESP8266"

    if platform.is_esp32():
        info["platform"] = "ESP32"

    if platform.is_esp32c3():
        info["platform"] = "ESP32C3"

    if platform.is_esp32s3():
        info["platform"] = "ESP32S3"

    return info["platform"]


# ESP32平台导入
if platform.is_esp32_series():
    import esp

    info["flash"]["size"] = esp.flash_size() / 1024


def get_ip():
    return wlan.get_ifconfig("ip")


def get_mask():
    return wlan.get_ifconfig("mask")


def get_dns():
    return wlan.get_ifconfig("dns")


def get_gateway():
    return wlan.get_ifconfig("gateway")


def get_flash_size(unit="KB"):
    """unit KB"""
    return info["flash"]["size"]


def get_temperature():
    if platform.is_esp8266():
        return -1

    if platform.is_raspberry_pico_series():
        return -1

    """获取mcu温度"""
    try:
        import esp32
        temp_value = -1
        fields = dir(esp32)
        if "raw_temperature" in fields:
            temp_value = esp32.raw_temperature()
        if "mcu_temperature" in fields:
            temp_value = esp32.mcu_temperature()
        return temp_value
    except Exception as e:
        print(e)


def gc_collect():
    gc.collect()


def get_gc_free_mem():
    return gc.mem_free()


def get_gc_alloc_mem():
    return gc.mem_alloc()


def update_boot_rtc(rtc_time):
    """更新启动时间,需要同步时间"""
    info["rtc"]["boot"] = rtc_time


def update_net_info():
    info["gc"]["alloc_m"] = get_gc_free_mem()
    info["gc"]["free_m"] = get_gc_alloc_mem()
    info["ifconfig"] = wlan.get_ifconfig()
    info["temperature"] = get_temperature()


def get_info():
    """获取设备信息前更新信息"""
    update_net_info()
    return info


### 首次时执行
get_info()
get_platform()

if __name__ == "__main__":
    print(info)
